package com.yita.demo01_base;

import java.sql.*;

public class Demo05_SelectByCondition {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.加载驱动程序
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://localhost:13306/fruitdb";
        String user = "root";
        String pwd = "root";
        //2.获取数据库连接
        Connection conn = DriverManager.getConnection(url,user,pwd);

        String sql = "select * from t_fruit where fid=?";

        //3.创建PreparedStatement对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        preparedStatement.setString(1,"1");

        //4.执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();

        //5.处理结果集
        while (resultSet.next()) {
            String fid = resultSet.getString("fid");
            String fname=resultSet.getString("fname");
            int price=resultSet.getInt("price");
            int fcount=resultSet.getInt("fcount");
            String remark = resultSet.getString("remark");
            System.out.printf("%s\t%-5s\t%d\t%d\t%s\t\n",fid,fname,price,fcount,remark);
        }

        //6.关闭资源
        resultSet.close();
        preparedStatement.close();
        conn.close();
    }
}
